
package com.pace.gonu.play;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import com.pace.gonu.constants.EndType;
import com.pace.gonu.constants.GameType;
import com.pace.gonu.constants.Order;
import com.pace.gonu.model.chnl.PlayChannel;
import com.pace.gonu.model.status.PlayStatusFactory;

/**
 * @author pace
 */
@Service
public class PlayService implements InitializingBean {
	private static ConcurrentHashMap<Integer, PlayChannel> channelMap;
	private final int MAX_CHANNEL = 50;

	public List<PlayChannel> getChannels() {
		List<PlayChannel> playChannels = new ArrayList<PlayChannel>();
		for (Iterator<Entry<Integer, PlayChannel>> iterator = channelMap.entrySet().iterator(); iterator.hasNext();) {
			Entry<Integer, PlayChannel> entry = iterator.next();
			PlayChannel playChannel = entry.getValue();
			playChannels.add(playChannel);
		}
		return playChannels;
	}

	public PlayChannel getChannel(int number) {
		return channelMap.get(number);
	}
	
	public PlayChannel getStartChannel() {
		for (Iterator<Entry<Integer, PlayChannel>> iterator = channelMap.entrySet().iterator(); iterator.hasNext();) {
			Entry<Integer, PlayChannel> entry = iterator.next();
			PlayChannel playChannel = entry.getValue();
			if (!playChannel.isSelectGameType() && !playChannel.isPlay()) {
				return playChannel;
			}
		}
		throw new IllegalArgumentException("사용자가 많아 현재 게임을 이용할 수 없습니다.");
	}

	public void initChannel(int number) {
		channelMap.put(number, new PlayChannel(number));
	}

	public String join(int number, String authKey, String name, String gameType) {
		PlayChannel playChannel = channelMap.get(number);
		//		playChannel.setSelectGameType(false);
		if (playChannel.isPlay()) {
			throw new IllegalArgumentException("게임중입니다.");
		}

		if (StringUtils.isBlank(playChannel.getFirstUser())) {
			playChannel.setGameType(GameType.valueOf(gameType));
			playChannel.setFirstUser(name);
			playChannel.setPlayStatus(PlayStatusFactory.getInstance(GameType.valueOf(gameType)));
			playChannel.setFirstAuthKey(authKey);
			return Order.RED.toString();
		} else {
			playChannel.setSecondUser(name);
			playChannel.setPlay(true);
			playChannel.setSecondAuthKey(authKey);
			return Order.BLUE.toString();
		}
		
	}

	public void setEndGame(int number, Order winner, EndType endType) {
		PlayChannel playChannel = channelMap.get(number);
		playChannel.setWinner(winner);
		playChannel.setEndType(endType);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		channelMap = new ConcurrentHashMap<Integer, PlayChannel>(MAX_CHANNEL);

		for (int i = 1; i <= MAX_CHANNEL; i++) {
			PlayChannel channel = new PlayChannel(i);
			channelMap.put(i, channel);
		}
	}

}
